package admin

import (
	"anban/service"
	"anban/utils"
	"html/template"
)

type DevicePictureController struct {
	BaseController
}

func (c *DevicePictureController) Add() {
	id := c.Ctx.Input.Param(":id")
	c.Data["deviceId"] = id
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/device_picture/add.html"
}

func (c *DevicePictureController) DoAdd() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if len(input["title"][0]) <= 0 {
		res = utils.ResJson(0, "请输入海报标题", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["url"][0]) <= 0 {
		res = utils.ResJson(0, "请上传海报图片", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	_, err := service.AddDevicePicture(input)
	if err != nil {
		res = utils.ResJson(0, "添加失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "添加成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *DevicePictureController) Edit() {
	id := c.Ctx.Input.Param(":id")

	p := map[string]interface{}{}
	p["id"] = id
	c.Data["record"] = service.GetDevicePictureInfo(p)

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/device_picture/edit.html"
}

func (c *DevicePictureController) DoEdit() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if len(input["title"][0]) <= 0 {
		res = utils.ResJson(0, "请输入海报标题", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if len(input["url"][0]) <= 0 {
		res = utils.ResJson(0, "请上传海报图片", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	service.DelPictureObj(input["id"][0])

	_, err := service.EditDevicePicture(input)
	if err != nil {
		res = utils.ResJson(0, "修改失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "修改成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *DevicePictureController) ShowAllList() {
	id := c.Ctx.Input.Param(":id")

	p := map[string]interface{}{}
	p["deviceId"] = id
	_, recordList := service.GetDevicePictureList(p)
	c.Data["recordList"] = recordList

	c.Data["deviceId"] = id
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/device_picture/all_list.html"
}

func (c *DevicePictureController) Del() {
	var res *utils.ResJsonStruct
	ids := c.GetString("ids")

	if len(ids) <= 0 {
		res = utils.ResJson(0, "请选择要删除的海报", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	service.DelPictureObj(ids)

	p := map[string]interface{}{}
	p["ids"] = ids
	_, err := service.DelDevicePicture(p)
	if err != nil {
		res = utils.ResJson(0, "删除失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	res = utils.ResJson(1, "删除成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}